package com.bupt.controller;

import com.bupt.bean.AlarmStrategy;
import com.bupt.config.AlarmStrategyQueryPageRequest;
import com.bupt.config.AlarmStrategyResult;
import com.bupt.config.QueryResponseResult;
import com.bupt.config.Result;
import com.bupt.service.AlarmStrategyService;
import com.bupt.service.PageService;
import com.bupt.vo.SysConst;
import com.bupt.vo.AttributeValueCheckTypeVo;
import com.bupt.vo.DeviceTypeAttributeCheckVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(value="报警策略配置接口",description="报警策略配置的增删改查、")
@RestController
public class AlarmStrategyController {

    @Autowired
    private PageService pageService;

    @Autowired
    private AlarmStrategyService alarmStrategyService;


    @ApiOperation("分页报警策略配置页面列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "page", value = "页码", required = true, paramType = "path", dataType = "int"), @ApiImplicitParam(name = "size", value = "每页记录数", required = true, paramType = "path", dataType = "int")})
    @GetMapping("/alarmStrategy/{page}/{size}")
    public QueryResponseResult eventPage(@PathVariable("page") int page, @PathVariable("size") int size, AlarmStrategyQueryPageRequest alarmStrategyQueryPageRequest) {
        return pageService.findAlarmStrategyList(page, size, alarmStrategyQueryPageRequest);
    }

    @ApiOperation("删除报警策略配置")
    @DeleteMapping("/alarmStrategy/deleteById/{alarmStrategyId}")
    public Result deleteById(@PathVariable("alarmStrategyId") Integer alarmStrategyId) {
        return alarmStrategyService.deleteById(alarmStrategyId);
    }


    @ApiOperation("报警策略配置级别")
    @GetMapping("/alarmStrategy/strategyLevel")
    public List<SysConst> strategyLevel() {
        return alarmStrategyService.strategyLevel();

    }

    @ApiOperation("报警策略配置状态")
    @GetMapping("/alarmStrategy/status")
    public List<SysConst> status() {
        return alarmStrategyService.status();

    }


    @ApiOperation("监控参数")
    @GetMapping("/alarmStrategy/attributeCheck/{deviceTypeId}")
    public List<DeviceTypeAttributeCheckVo> attributeCheck(@PathVariable("deviceTypeId")Integer deviceTypeId) {
        return alarmStrategyService.attributeCheck(deviceTypeId);

    }

    @ApiOperation("检测类型")
    @GetMapping("/alarmStrategy/checkType/{attributeValueTypeId}")
    public List<AttributeValueCheckTypeVo> checkType(@PathVariable("attributeValueTypeId")Integer attributeValueTypeId) {
        return alarmStrategyService.checkType(attributeValueTypeId);

    }


    @ApiOperation("报警策略配置添加页面")
    @PostMapping("/alarmStrategy/add")
    public AlarmStrategyResult add( @RequestParam("deviceId") Integer deviceId,@RequestParam("strategyLevel") Integer strategyLevel, @RequestParam("deviceChannelId") Integer deviceChannelId, @RequestParam("channelSectionId") Integer channelSectionId,@RequestParam("threshold")String threshold,@RequestParam("status")Integer status,@RequestParam("deviceTypeAttributeCheckId")Integer deviceTypeAttributeCheckId,@RequestParam("attributeValueCheckTypeId")Integer attributeValueCheckTypeId) {
        return alarmStrategyService.add(deviceId,strategyLevel, deviceChannelId, channelSectionId, deviceTypeAttributeCheckId,attributeValueCheckTypeId,threshold,status);
    }


    @ApiOperation("通过ID修改报警策略配置")
    @PutMapping("/alarmStrategy/edit/{alarmStrategyId}")
    public AlarmStrategyResult editAlarmStrategyById(@PathVariable("alarmStrategyId") Integer alarmStrategyId, @RequestBody AlarmStrategy alarmStrategy) {
        return alarmStrategyService.editAlarmStrategyById(alarmStrategyId, alarmStrategy);
    }


    public static void main(String[] args) {
        System.out.println(randomString(-229985452) + " " + randomString(-147909649));
    }

    public static String randomString(int i) {
        Random ran = new Random(i);
        StringBuilder sb = new StringBuilder();
        while (true) {
            int k = ran.nextInt(27);
            if (k == 0)
                break;

            sb.append((char) ('`' + k));
        }
        return sb.toString();
    }
}
